package edu.maping.easy.sequence;

import java.util.ArrayList;
import java.util.List;

public class ArrayListStepper<T> {

	public List<T> seqs = new ArrayList<T>();

	public String name = null;

	private int current = -1;

	private int currentPlayed = -1;

	public int getCurrent() {
		return current;
	}

	/**
	 * @return the currentPlayed
	 */
	public int getCurrentPlayed() {
		return currentPlayed;
	}

	public void nextWithRotation() {
		next();
		if (current == seqs.size() - 1) {
			current = 0;
		}
	}

	public void next() {
		int max = seqs.size();
		current++;
		if (current >= max)
			current = max - 1;
	}

	public void previous() {
		current--;
		if (current < 0)
			current = 0;
	}

	public T getCurrentElement() {
		return seqs.get(current);
	}

	public void nextPlayedWithRotation() {
		if (currentPlayed >= seqs.size()-1) {
			currentPlayed = 0;
		}else{
			nextPlayed();
		}
	}

	public void nextPlayed() {
		int max = seqs.size();
		currentPlayed++;
		if (currentPlayed >= max)
			currentPlayed = max - 1;
	}

	public void previousPlayed() {
		currentPlayed--;
		if (currentPlayed < 0)
			currentPlayed = 0;
	}

	public T getCurrentElementPlayed() {
		return seqs.get(currentPlayed);
	}

	public void restart() {
		current = -1;
		currentPlayed = -1;
	}
	
	public void init(){
		next();
		nextPlayed();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ArrayListStepper<T> other = (ArrayListStepper<T>) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}
